package sgi.client;

import ocsf.client.*;
import java.io.*;

import javax.swing.JOptionPane;

import sgi.message.*;

public class SGIClientConnectionController extends AbstractClient{
	/**
	 * @param args
	 * @throws IOException 
	 */
	
	private static SGIClientConnectUI clientUI;
	private static SGIClientConnectionController connection;
	
	/**create the controller as singleton - only one instance if allowed.*/
	public static SGIClientConnectionController createConnection() throws IOException
	{
		if(connection == null)
		{
				connection = new SGIClientConnectionController();
				clientUI.setClient(connection);
		}
		
		return connection;
	}
	
	/**private empty constructor  to use for create.*/
	private SGIClientConnectionController() throws IOException  
	{
		 super(); //Call the superclass constructor
		 SGIClientConnectionController.clientUI=new SGIClientConnectUI();
		 clientUI.setVisible(true);
	}
	
	/**private constructor  to use for create.*/
	private SGIClientConnectionController(String host,int port,SGIClientConnectUI clientUI) throws IOException  
	{
		 super(host, port); //Call the superclass constructor
		 SGIClientConnectionController.clientUI=clientUI;
		 clientUI.setVisible(true);
		 try {
			 	openConnection();
		 } catch (IOException e) {clientUI.setNotification("Connection to server failed."); }
		// clientUI.setNotification("Connection to server succeeded.");
	}
	
	/**Send the server response to the initiator of the message*/
	public void handleMessageFromServer(Object msg) 		
	{
		if(msg instanceof MessageLogin)
			try {
				(SGILoginController.createLogin()).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("Login handling exception.");}
		else if(msg instanceof MessageRegister)
		{
			(SGIRegisterController.createRegister()).handleMessage(msg);
		}
		else if(msg instanceof MessageComplaint)
		{
			try {
				(SGIComplaintController.create(null)).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("Complaint handling exception.");}
		}
		else if(msg instanceof MessageSearch)
		{
			try {
				if (((MessageSearch) msg).getSearchMessageType() == 1)
					(SGISearchArchiveController.create()).handleMessage(msg);
				else /**if ((MessageSearch) msg).getSearchMessageType() == 2)*/
					(SGIInteractiveMapController.create()).handleMessage(msg);
			} catch (IOException e) 
				{	
					if (((MessageSearch) msg).getSearchMessageType() == 1){
						JOptionPane.showMessageDialog(SGISearchArchiveController.getUI(),"MessageSearch handling failure");	
						e.printStackTrace();
					}
					else {/**if ((MessageSearch) msg).getSearchMessageType() == 2)*/
						JOptionPane.showInputDialog(SGIInteractiveMapController.getUI(),"MessageSearch handling failure"); 
						e.printStackTrace();
					}
				}
		}
		else if(msg instanceof MessageGetComplaint)
		{
			try {
				(SGIComplaintReviewController.create()).handleMessage(msg);
			} catch (IOException e) {	JOptionPane.showMessageDialog(SGIComplaintReviewController.getUI(),"GetComplaint handling failure");	; e.printStackTrace();}
		}
		else if(msg instanceof MessageViewInfo)
		{
			try {
				(SGIAccountInfoController.create(null)).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("AccountInfo handling exception.");}			
		}
		else if(msg instanceof MessagePurchaseImage)
		{
			try {
				(SGIPurchaseImageController.create(null,null)).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("Image handling exception.");}
		}
		else if(msg instanceof MessagePurchaseSample)
		{
			try {
				(SGIPurchaseSampleController.create(null,null)).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("Sample handling exception.");}
		}
		else if(msg instanceof MessageDataLayer)
		{
			(SGISubscribeController.createSubscribe()).handleMessageDataLayer(msg);
		}
		else if(msg instanceof MessageDateTime)
		{
			(SGISubscribeController.createSubscribe()).handleMessageDateTime(msg);
		}
		else if(msg instanceof MessageImagePack)
		{
			(SGISubscribeController.createSubscribe()).handleMessageImagePack(msg);
		}
		else if(msg instanceof MessageImageAdd)
		{
			try {
				(SGIImageAddController.create()).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("Image Add handling exception.");}			
		}
		else if(msg instanceof MessageGetDataLayer)
		{
			try {
				(SGIReviewSubscriptionsController.createSubscriptions()).handleMessagefromDataLayer(msg);
			} catch (IOException e) {	JOptionPane.showMessageDialog(SGIReviewSubscriptionsController.getUI(),"GetDataLayer handling failure");	; e.printStackTrace();}
		}
		else if(msg instanceof MessageGetDateTime)
		{
			try {
				(SGIReviewSubscriptionsController.createSubscriptions()).handleMessagefromDateTime(msg);
			} catch (IOException e) {	JOptionPane.showMessageDialog(SGIReviewSubscriptionsController.getUI(),"GetDateTime handling failure");	; e.printStackTrace();}
		}
		else if(msg instanceof MessageGetImagePack)
		{
			try {
				(SGIReviewSubscriptionsController.createSubscriptions()).handleMessagefromImagePack(msg);
			} catch (IOException e) {	JOptionPane.showMessageDialog(SGIReviewSubscriptionsController.getUI(),"GetImagePack handling failure");	; e.printStackTrace();}
		}
		else if(msg instanceof MessageAcceptDataLayer)
		{
			try {
				(SGIReviewSubscriptionsController.createSubscriptions()).handleMessage(msg);
			} catch (IOException e) {	JOptionPane.showMessageDialog(SGIReviewSubscriptionsController.getUI(),"AcceptDataLayer handling failure");	; e.printStackTrace();}
		}
		else if(msg instanceof MessageAcceptDateTime)
		{
			try {
				(SGIReviewSubscriptionsController.createSubscriptions()).handleMessage(msg);
			} catch (IOException e) {	JOptionPane.showMessageDialog(SGIReviewSubscriptionsController.getUI(),"AcceptDateTime handling failure");	; e.printStackTrace();}	
		}
		else if(msg instanceof MessageAcceptImagePack)
		{
			try {
				(SGIReviewSubscriptionsController.createSubscriptions()).handleMessage(msg);
			} catch (IOException e) {	JOptionPane.showMessageDialog(SGIReviewSubscriptionsController.getUI(),"AcceptImagePack handling failure");	; e.printStackTrace();}
		}
		else if(msg instanceof MessageViewMessages)
		{
			try {
				(SGIMessagesController.create()).handleMessage(msg);
			} catch (IOException e) {	JOptionPane.showMessageDialog(SGIReviewSubscriptionsController.getUI(),"View Messages handling failure");	; e.printStackTrace();}
		}
		else if(msg instanceof MessageRetrieveTariff){
			if(((MessageRetrieveTariff)msg).isSellManager()){
				SGITariffController.create().handleMessage(msg);
			}else{
				SGICEOTariffController.create().handleMessage(msg);
			}
		}
		else if(msg instanceof MessageApproveTariff){
			SGITariffController.create().handleMessage(msg);
		}
		else if(msg instanceof MessageHistory)
		{
			try {
				(SGIAccountInfoController.create(null)).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("AccountInfo handling exception.");}			
		}
		else if(msg instanceof MessageImageDelete)
		{
			try {
				(SGIImageDeleteController.create()).handleMessage(msg);
			} catch (IOException e) {	JOptionPane.showMessageDialog(SGIReviewSubscriptionsController.getUI(),"View Images handling failure");	; e.printStackTrace();}
		}
		else if(msg instanceof MessageReviewImages)
		{
			try {
				(SGIImageEditController.create()).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("Image Review for edit handling exception.");}			
		}
		else if(msg instanceof MessageGetImageEdit)
		{
			try {
				(SGIImageEditController.create()).handleMessageImageFullDetails(msg);
			} catch (IOException e) {clientUI.setNotification("Image full details for edit handling exception.");}		
		}
		else if(msg instanceof MessageTariffExtract)
		{
			try {
				(SGIViewTariffController.create()).handleMessage(msg);
			} catch (IOException e) {clientUI.setNotification("Tariff presenting handling exception.");}	
		}
		else if(msg instanceof MessageRetrieveReport)
		{
			(SGICheckReportsController.create()).handleMessage(msg);	
		}
		else
		{
			clientUI.setNotification("Message type unknown");
		}
		
				
	}
	
	
	/**Terminate server connection*/
	public void quit()
	{
	  try
	  {
	     closeConnection();
	  }
	  catch(IOException e) {e.printStackTrace();}
	  System.exit(0);
	}
	
	  public void handleMessageFromClientUI(Message msg) throws IOException
	  {
	    try
	    {
	    	sendToServer(msg);
	    }
	    catch(IOException e)
	    {
	      clientUI.setNotification("Could not send message to server.");
	      throw e;
	    }
	  }
	  
	  /**initialize server connection*/
	  public void startConnection(String host,int port) throws IOException
	  {
			this.setHost(host);
			this.setPort(port);
			 try {
				 	this.openConnection();
				 	//setNotification("Connection to server succeeded.");
				 	this.handleMessageFromClientUI(new MessagePrompt("Client connected."));
				 	clientUI.setVisible(false);
				 	SGILoginController.createLogin();
			 } catch (IOException e) {throw e;}
	  }
	  
}
